package com.lynchcms.cms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lynchcms.cms.dao.CmsAdminDao;
import com.lynchcms.cms.dao.CmsAdminRoleDao;
import com.lynchcms.cms.dao.CmsRoleDao;
import com.lynchcms.cms.model.entity.CmsAdmin;
import com.lynchcms.cms.model.entity.CmsAdminRole;
import com.lynchcms.cms.model.entity.CmsRole;
import com.lynchcms.cms.service.CmsAdminSev;
import com.lynchcms.common.page.Pager;
import com.lynchcms.core.service.impl.BaseServiceImpl;

/**
 * 管理员业务处理Sev--Impl
 * 
 * @author Lynch
 */
@Service("cmsAdminSev")
public class CmsAdminSevImpl extends BaseServiceImpl<CmsAdmin, Long> implements
		CmsAdminSev {

	private CmsAdminDao cmsAdminDao;
	private CmsAdminRoleDao cmsAdminRoleDao;
	private CmsRoleDao cmsRoleDao;

	public Map<String, Object> pagerListTest(int dATANUMBER, Integer page,
			CmsAdmin cmsAdmin) {

		String hql = "FROM CmsAdmin AS ca WHERE 1=1 ";
		Map<String, Object> param = new HashMap<String, Object>();
		if (cmsAdmin.getAdminName() != null
				&& !"".equals(cmsAdmin.getAdminName())) {
			hql = hql + " AND ca.adminName like:a";
			param.put("a", '%' + cmsAdmin.getAdminName() + '%');
		}
		// if(cmsAdmin.getAdminAge()!=null&&cmsAdmin.getAdminAge()>0){
		// hql = hql + " AND ca.adminAge = :b";
		// param.put("b",cmsAdmin.getAdminAge());
		// }
		// if(cmsAdmin.getCreateTime()!=null&&!"".equals(cmsAdmin.getCreateTime())){
		// hql = hql + " AND ca.createTime > :c AND ca.createTime <:d";
		// param.put("c", cmsAdmin.getCreateTime());
		// param.put("d", DateUtil.TomorrowToDate(cmsAdmin.getCreateTime()));
		// }
//		System.out.println("hql=" + hql);
		Pager<CmsAdmin> pager = this.pagerList(dATANUMBER, page, hql, param);
		Map<String, Object> result = new HashMap<String, Object>(2);
		result.put("total", pager.getAllRow());
		result.put("rows", pager.getList());
		return result;
	}
	
	public void updateAdminRole(Long adminId, List<Long> roleIds) {

		//删除掉中间表所有此用户的所有的数据
		cmsAdminRoleDao.deleteAdminRole(adminId);
		//得到用户对象
		CmsAdmin cmsAdmin = cmsAdminDao.get(adminId);
		//存放角色资源的set
		Set<CmsAdminRole> cmsAdminRoles = new HashSet<CmsAdminRole>();
		//循环将创建所有的用户角色信息
//		for(int i=0;i<roleIdStr.length;i++){
//			Role role = roleDao.get(Long.parseLong(roleIdStr[i]));
//			UserRole userRole = new UserRole();
//			userRole.setRole(role);
//			userRole.setUser(user);
//			userRole.setEnabled(true);
//			userRoles.add(userRole);
//		}
		Iterator<Long> iterator = roleIds.iterator();
		while (iterator.hasNext()) {
			Long roleId = iterator.next();
			CmsRole cmsRole = cmsRoleDao.get(roleId);
			CmsAdminRole cmsAdminRole = new CmsAdminRole();
			cmsAdminRole.setCmsAdmin(cmsAdmin);
			cmsAdminRole.setCmsRole(cmsRole);
			cmsAdminRole.setAdminRoleCreateTime(new Date()); 
			cmsAdminRole.setEnabled(true);
			cmsAdminRoles.add(cmsAdminRole);
		}
//		user.setUserRoles(userRoles);
		cmsAdmin.setAdminRoles(cmsAdminRoles);
		cmsAdminDao.update(cmsAdmin);
//		userDao.update(user);
	}

	public CmsAdminDao getCmsAdminDao() {
		return cmsAdminDao;
	}

	@Resource
	public void setCmsAdminDao(CmsAdminDao cmsAdminDao) {
		super.setBaseDao(cmsAdminDao);
		this.cmsAdminDao = cmsAdminDao;
	}

	public CmsAdminRoleDao getCmsAdminRoleDao() {
		return cmsAdminRoleDao;
	}

	@Resource
	public void setCmsAdminRoleDao(CmsAdminRoleDao cmsAdminRoleDao) {
		this.cmsAdminRoleDao = cmsAdminRoleDao;
	}

	public CmsRoleDao getCmsRoleDao() {
		return cmsRoleDao;
	}

	@Resource
	public void setCmsRoleDao(CmsRoleDao cmsRoleDao) {
		this.cmsRoleDao = cmsRoleDao;
	}

	
	

}
